package com.hong.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
@ComponentScan(basePackages = { "com.hong.email" })
@PropertySource(value = "classpath:email.properties",ignoreResourceNotFound = false)
public class EmailConfig {

    @Autowired
    private Environment environment;


    public static  String FROM ;
    public static  String TO;
    public static  String SUBJECT = "Spring Email Test";
    public static  String TEXT = "Hello i am hong";
    //文件路径
    public static  String FILE_PATH = "D:\\ideaWorkSpace\\spring\\spring_email\\image\\test.png";

	/**
	 * 配置邮件发送器
	 * @return
	 */
    @Bean
    public MailSender mailSender() {
        FROM= environment.getProperty("email.username");
        TO= environment.getProperty("email.to");
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost(environment.getProperty("email.smtp"));//指定用来发送Email的邮件服务器主机名
        mailSender.setPort(25);//默认端口，标准的SMTP端口
        mailSender.setUsername(environment.getProperty("email.username"));//用户名
        mailSender.setPassword(environment.getProperty("email.password"));//密码
        return mailSender;
    }

}